import { _decorator, randomRangeInt, Vec2 } from 'cc';
import CardFunctions from "../Common/CardFunctions";
import BlockGroup from "../Game/BlockGroup";
import Board from "../Game/Board";
import Card from "../Game/Card";
import StopBlock from "../Game/StopBlock";
import SS_AssetsManager from "../Manager/SS_AssetsManager";
import { MainState } from "./GameEnum";
import GameStorage from "./GameStorage";
import Grid from '../Game/Grid';

// /** 游戏数据 */
export class GameData {
    static row: number = 6; // 行数
    static col: number = 6; // 列数
    //    // static size: number = 50; // 方块的尺寸
    static cell_width: number = 80;
    static cell_height: number = 80;
    static W: number = GameData.cell_width * GameData.col;
    static H: number = GameData.cell_height * GameData.row;
    static card_width: number = 80;
    static card_height: number = 80;
    static arrGrids: any[][] = [];
    static arrCards: Card[] = [];
    static arrLines: Grid[] = [];

    static arrCardGroups: BlockGroup[] = [];
    static arrBlockGroup_M: BlockGroup[] = [];
    static arrCardGroups_temp: BlockGroup[] = [];
    static arrBoard: Board[] = [];
    static arrStopBlock: StopBlock[] = [];
    static CurrentLevel = 0;
    static CurrentLevelData;
    static 装填list = [];
    static 撤凳子list = [];
    static currentBlockGroupCreateIndex: number = 0;
    static currentLevelData_d = [];
    static currentUnlockId = null;
    static mainState = MainState.空闲;
    static TetrisData = [
        { tetris_id: 0, count: 1, name: "1格方块" },
        { tetris_id: 1, count: 2, name: "2格长条" },
        { tetris_id: 2, count: 3, name: "3格长条" },
        { tetris_id: 3, count: 3, name: "3格L" },
        { tetris_id: 4, count: 4, name: "4格长条" },
        { tetris_id: 5, count: 4, name: "4格方块" },
        { tetris_id: 6, count: 4, name: "4格L" },
        { tetris_id: 7, count: 4, name: "4格Z" },
        { tetris_id: 8, count: 4, name: "4格T" },
    ]
    static StartPos: Vec2;
    static CurrentPos: Vec2;
    static IsMouseDown: boolean = false;
    static gamePause = false;
    static isFirstEnterMainScene = true;
    static total_card_count: number = 0;
    static destroy_card_count: number = 0;
    static 连击time: number = null;
    static 连击count: number = null;
    static 是否正在消除: boolean = false;
    static 是否等待消除: boolean = false;
    static UnlockData = [
        [
            { x: 5, y: 0 },
            { x: 5, y: 1 },
            { x: 4, y: 0 },
            { x: 4, y: 1 },
        ],
        [
            { x: 5, y: 2 },
            { x: 5, y: 3 },
            { x: 4, y: 2 },
            { x: 4, y: 3 },
        ],
    ]
    static LevelData = {};
    static Current_next_BlockGroup = null;
    static Offset_y_each_layer = 8;
    static 普通关卡数量 = 40;
    static 本关剩余复活次数: number = 1;
    //    // static gameState;
    //    // /** 是否可以操作 */
    //    // static IsCanOperate: boolean = true;
    static 本回合是否完成消除 = false;

    static async InitData() {
        GameData.arrGrids = [];
        GameData.arrCards = [];
        GameData.arrCardGroups = [];
        GameData.arrBlockGroup_M = [];
        GameData.arrCardGroups_temp = [];
        GameData.arrBoard = [];
        GameData.arrStopBlock = [];

        GameData.arrLines = [];


        // GameData.CurrentLevel = 0;
        GameData.CurrentLevelData = [];

        GameData.装填list = [];
        GameData.撤凳子list = [];

        GameData.currentBlockGroupCreateIndex = 0;
        GameData.currentLevelData_d = [];

        GameData.mainState = MainState.空闲;

        GameData.total_card_count = 0;
        GameData.destroy_card_count = 0;

        GameData.currentUnlockId = null;

        GameData.连击time = null;
        GameData.连击count = null;

        GameData.是否正在消除 = false;
        GameData.是否等待消除 = false;

        GameData.Current_next_BlockGroup = null;

        GameData.本关剩余复活次数 = 1;

        GameData.Set_LevelData();
    }

    public static Get_AllLevelData(): any {
        return SS_AssetsManager.Levels;
    }

    public static Get_CurrentLevel() {
        return GameData.CurrentLevel;
    }

    public static Set_LevelData() {
        let isWujin = CardFunctions.是否是无尽模式(GameData.Get_CurrentLevel())

        if (isWujin) {
            if (GameStorage.data.wujin_data === null) {
                CardFunctions.生成无尽模式数据();
            }
            GameData.LevelData = GameStorage.data.wujin_data;
        }
        else {
            let levels = GameData.Get_AllLevelData();
            GameData.LevelData = levels[GameData.Get_CurrentLevel()];
        }
    }

    public static Get_LevelData(): any {
        let levelData = GameData.LevelData;
        return levelData;
    }

    public static Get_LevelData_up() {
        let levelData = GameData.Get_LevelData();
        let levelData_u = levelData.u
        return levelData_u;
    }

    public static Get_LevelData_middle() {
        let levelData = GameData.Get_LevelData();
        return levelData.m;
    }

    public static Get_LevelData_up_无尽() {
        let array = [];
        for (let index = 0; index < SS_AssetsManager.Levels.length; index++) {
            const Level = SS_AssetsManager.Levels[index].u;
            // console.log("Level", Level);

            for (let index_2 = 0; index_2 < Level.length; index_2++) {
                const layer = Level[index_2];
                // console.log("layer", layer);
                array.push(layer);
            }
        }

        // console.log("array", array);

        let count = 3;

        let wujin = [];
        for (let index = 0; index < count; index++) {
            const random = randomRangeInt(0, array.length);
            let data = array[random];
            wujin.push(data);
        }

        // console.log("wujin", wujin);

        return wujin;
    }

    public static Get_LevelData_down() {
        return GameData.currentLevelData_d;
    }
}

/** 调试 */
window["GameData"] = GameData;